// DlgSearchRequest.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgSearchRequest.h"


// CDlgSearchRequest dialog

IMPLEMENT_DYNAMIC(CDlgSearchRequest, CDialog)

CDlgSearchRequest::CDlgSearchRequest(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSearchRequest::IDD, pParent)
	, m_bMore(FALSE)
	, m_szFilter(_T(""))
	, m_szName(_T(""))
	, m_szEmail(_T(""))
	, m_szLocation(_T(""))
	, m_szPhone(_T(""))
	, m_szFax(_T(""))
	, m_szInstructions(_T(""))
	, m_szCitation(_T(""))
	, m_szNotes(_T(""))
	, m_szRequestID(_T(""))
	, m_szLANID(_T(""))
{
	m_nStatus = -1;
	m_nLibrary = -1;
	m_pszaLibrary = NULL;
	m_pszaStatus = NULL;
}

CDlgSearchRequest::~CDlgSearchRequest()
{
}

void CDlgSearchRequest::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_NAME, m_szName);
	DDX_Text(pDX, IDC_EDIT_EMAIL, m_szEmail);
	DDX_Text(pDX, IDC_EDIT_LOCATION, m_szLocation);
	DDX_Text(pDX, IDC_EDIT_PHONE, m_szPhone);
	DDX_Text(pDX, IDC_EDIT_FAX, m_szFax);
	DDX_Text(pDX, IDC_EDIT_INSTRUCTIONS, m_szInstructions);
	DDX_Text(pDX, IDC_EDIT_CITATION, m_szCitation);
	DDX_Text(pDX, IDC_EDIT_STAFF_NOTE, m_szNotes);
	DDX_Text(pDX, IDC_EDIT_REQUEST_ID, m_szRequestID);
	DDX_Control(pDX, IDC_COMBO_STATUS, m_ctrlStatus);
	DDX_Control(pDX, IDC_DATETIMEPICKER_FIRST, m_ctlDateFirst);
	DDX_Control(pDX, IDC_DATETIMEPICKER_LAST, m_ctlDateLast);
	DDX_Control(pDX, IDC_COMBO_LIBRARY, m_ctrlLibrary);
	DDX_Text(pDX, IDC_EDIT_LAN_ID, m_szLANID);
}


BEGIN_MESSAGE_MAP(CDlgSearchRequest, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_MORE, &CDlgSearchRequest::OnBnClickedButtonMore)
	ON_BN_CLICKED(IDC_BUTTON_CLEAR, &CDlgSearchRequest::OnBnClickedButtonClear)
END_MESSAGE_MAP()


// CDlgSearchRequest message handlers

BOOL CDlgSearchRequest::OnInitDialog()
{
	CDialog::OnInitDialog();

	ASSERT( m_pszaLibrary != NULL );
	ASSERT( m_pszaStatus != NULL );

	// get settings
	m_bMore = AfxGetApp()->GetProfileInt( _T("Search"), _T("More"), 0 );
	m_szName = AfxGetApp()->GetProfileString( _T("Search"), _T("Name") );
	m_szEmail = AfxGetApp()->GetProfileString( _T("Search"), _T("Email") );
	m_szLocation = AfxGetApp()->GetProfileString( _T("Search"), _T("Location") );
	m_szPhone = AfxGetApp()->GetProfileString( _T("Search"), _T("Phone") );
	m_szFax = AfxGetApp()->GetProfileString( _T("Search"), _T("Fax") );
	m_szInstructions = AfxGetApp()->GetProfileString( _T("Search"), _T("Instructions") );
	m_szCitation = AfxGetApp()->GetProfileString( _T("Search"), _T("Citation") );
	m_szNotes = AfxGetApp()->GetProfileString( _T("Search"), _T("Notes") );
	m_szRequestID = AfxGetApp()->GetProfileString( _T("Search"), _T("ID") );
	m_nStatus = AfxGetApp()->GetProfileInt( _T("Search"), _T("Status"), -1 );
	m_nLibrary = AfxGetApp()->GetProfileInt( _T("Search"), _T("Library"), -1 );

	INT nCurSel = 0;
	INT nSel = 0;
	CString szTemp;

	// set m_ctrlStatus
	nSel = m_ctrlStatus.AddString( _T("") );
	m_ctrlStatus.SetItemData( nSel, -1 );
	for( INT nIndex = 0; nIndex < m_pszaStatus->GetCount(); nIndex++ )
	{
		szTemp = m_pszaStatus->GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlStatus.AddString( szTemp );
			m_ctrlStatus.SetItemData( nSel, nIndex );
			if( m_nStatus == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlStatus.SetCurSel( nCurSel );

	// set m_ctrlLibrary
	nSel = m_ctrlLibrary.AddString( _T("") );
	m_ctrlLibrary.SetItemData( nSel, -1 );
	for( INT nIndex = 0; nIndex < m_pszaLibrary->GetCount(); nIndex++ )
	{
		szTemp = m_pszaLibrary->GetAt( nIndex );
		if( !szTemp.IsEmpty() )
		{
			nSel = m_ctrlLibrary.AddString( szTemp );
			m_ctrlLibrary.SetItemData( nSel, nIndex );
			if( m_nLibrary == nIndex )
				nCurSel = nSel;
		}
	}
	m_ctrlLibrary.SetCurSel( nCurSel );

	// set check
	m_ctlDateFirst.SetTime();
	m_ctlDateLast.SetTime();

	// adjust for no LAN ID
	if( !appConfig.m_LDAP.bUseLDAPLogons )
	{
		GetDlgItem( IDC_STATIC_LAN_ID )->ShowWindow( FALSE );
		GetDlgItem( IDC_EDIT_LAN_ID )->ShowWindow( FALSE );
	}

	// size controls
	SizeControls();

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgSearchRequest::OnOK()
{
	CWaitCursor wait;

	// update data
	UpdateData();

	// trim variables
	m_szFilter.Trim();
	m_szName.Trim();
	m_szEmail.Trim();
	m_szLocation.Trim();
	m_szPhone.Trim();
	m_szFax.Trim();
	m_szInstructions.Trim();
	m_szCitation.Trim();
	m_szNotes.Trim();
	m_szRequestID.Trim();

	CString szQuery;
	CString szTemp;

	// get library id
	m_nLibrary = m_ctrlLibrary.GetItemData( m_ctrlLibrary.GetCurSel() );

	// get status id
	m_nStatus = m_ctrlStatus.GetItemData( m_ctrlStatus.GetCurSel() );

	//build search string
	if( !m_szRequestID.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("requests.id = %ld"), _tstol( m_szRequestID ) );
		m_szFilter += szQuery;
	}
	if( !m_szName.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("users.name LIKE '%%%s%%'"), ES(m_szName) );
		CArray<USER_SET> aUser;
		appConfig.m_SQL.GetUserSet( &aUser, szQuery );
		// check for user
		CString szUsers;
		if( aUser.GetCount() > 0 )
		{
			// add users
			for( INT nIndex = 0; nIndex < aUser.GetCount(); nIndex++ )
			{
				if( !szUsers.IsEmpty() )
					szTemp.Format( _T(" OR requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				else
					szTemp.Format( _T("(requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				szUsers += szTemp;
			}
			szUsers += _T(") ");
			m_szFilter += szUsers;
		}
		else
		{
			// no user found
			m_szFilter += _T("(requests.user_id=-1)");
		}
	}
	if( !m_szEmail.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("users.email LIKE '%%%s%%'"),ES( m_szEmail) );
		CArray<USER_SET> aUser;
		appConfig.m_SQL.GetUserSet( &aUser, szQuery );
		// check for user
		CString szUsers;
		if( aUser.GetCount() > 0 )
		{
			// add users
			for( INT nIndex = 0; nIndex < aUser.GetCount(); nIndex++ )
			{
				if( !szUsers.IsEmpty() )
					szTemp.Format( _T(" OR requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				else
					szTemp.Format( _T("(requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				szUsers += szTemp;
			}
			szUsers += _T(") ");
			m_szFilter += szUsers;
		}
		else
		{
			// no user found
			m_szFilter += _T("(requests.user_id=-1)");
		}
	}
	if( !m_szLANID.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("users.lan_id LIKE '%%%s%%'"),ES( m_szLANID ) );
		CArray<USER_SET> aUser;
		appConfig.m_SQL.GetUserSet( &aUser, szQuery );
		// check for user
		CString szUsers;
		if( aUser.GetCount() > 0 )
		{
			// add users
			for( INT nIndex = 0; nIndex < aUser.GetCount(); nIndex++ )
			{
				if( !szUsers.IsEmpty() )
					szTemp.Format( _T(" OR requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				else
					szTemp.Format( _T("(requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				szUsers += szTemp;
			}
			szUsers += _T(") ");
			m_szFilter += szUsers;
		}
		else
		{
			// no user found
			m_szFilter += _T("(requests.user_id=-1)");
		}
	}
	if( !m_szLocation.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("users.address1 LIKE '%%%s%%' AND users.address2 LIKE '%%%s%%'"), ES(m_szLocation), ES(m_szLocation) );
		CArray<USER_SET> aUser;
		appConfig.m_SQL.GetUserSet( &aUser, szQuery );
		// check for user
		CString szUsers;
		if( aUser.GetCount() > 0 )
		{
			// add users
			for( INT nIndex = 0; nIndex < aUser.GetCount(); nIndex++ )
			{
				if( !szUsers.IsEmpty() )
					szTemp.Format( _T(" OR requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				else
					szTemp.Format( _T("(requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				szUsers += szTemp;
			}
			szUsers += _T(") ");
			m_szFilter += szUsers;
		}
		else
		{
			// no user found
			m_szFilter += _T("(requests.user_id=-1)");
		}
	}
	if( !m_szPhone.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("users.phone LIKE '%%%s%%'"),ES( m_szPhone) );
		CArray<USER_SET> aUser;
		appConfig.m_SQL.GetUserSet( &aUser, szQuery );
		// check for user
		CString szUsers;
		if( aUser.GetCount() > 0 )
		{
			// add users
			for( INT nIndex = 0; nIndex < aUser.GetCount(); nIndex++ )
			{
				if( !szUsers.IsEmpty() )
					szTemp.Format( _T(" OR requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				else
					szTemp.Format( _T("(requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				szUsers += szTemp;
			}
			szUsers += _T(") ");
			m_szFilter += szUsers;
		}
	}
	if( !m_szFax.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("users.fax LIKE '%%%s%%'"), ES(m_szFax) );
		CArray<USER_SET> aUser;
		appConfig.m_SQL.GetUserSet( &aUser, szQuery );
		// check for user
		CString szUsers;
		if( aUser.GetCount() > 0 )
		{
			// add users
			for( INT nIndex = 0; nIndex < aUser.GetCount(); nIndex++ )
			{
				if( !szUsers.IsEmpty() )
					szTemp.Format( _T(" OR requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				else
					szTemp.Format( _T("(requests.user_id=%ld"), aUser.GetAt( nIndex ).m_id );
				szUsers += szTemp;
			}
			szUsers += _T(") ");
			m_szFilter += szUsers;
		}
		else
		{
			// no user found
			m_szFilter += _T("(requests.user_id=-1)");
		}
	}
	if( !m_szInstructions.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("requests.instructions LIKE '%%%s%%'"), ES(m_szInstructions) );
		m_szFilter += szQuery;
	}
	if( !m_szCitation.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("requests.request LIKE '%%%s%%'"), ES(m_szCitation) );
		m_szFilter += szQuery;
	}
	if( !m_szNotes.IsEmpty() )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("requests.information LIKE '%%%s%%'"), ES(m_szNotes) );
		m_szFilter += szQuery;
	}
	if( m_nLibrary > -1 )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("requests.library_id = %d"), m_nLibrary );
		m_szFilter += szQuery;
	}
	if( m_nStatus > -1 )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("requests.status_id = %d"), m_nStatus );
		m_szFilter += szQuery;
	}

	CTime time;
	// get date start
	if( m_ctlDateFirst.GetTime( time ) != GDT_NONE )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("requests.date_in >= '%s'"), time.Format( _T("%Y-%m-%d 00:00:00") ));
		m_szFilter += szQuery;
	}

	// get date end
	if( m_ctlDateLast.GetTime( time ) != GDT_NONE )
	{
		if( !m_szFilter.IsEmpty() )
			m_szFilter += _T(" AND ");
		szQuery.Format( _T("requests.date_in <= '%s'"), time.Format( _T("%Y-%m-%d 23:59:59") ));
		m_szFilter += szQuery;
	}

	// save settings
	AfxGetApp()->WriteProfileInt( _T("Search"), _T("More"), m_bMore );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("Name"), m_szName );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("Email"), m_szEmail );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("Location"), m_szLocation );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("Phone"), m_szPhone );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("Fax"),m_szFax );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("Instructions"), m_szInstructions );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("Citation"), m_szCitation );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("Notes"), m_szNotes );
	AfxGetApp()->WriteProfileString( _T("Search"), _T("ID"), m_szRequestID );
	AfxGetApp()->WriteProfileInt( _T("Search"), _T("Status"), m_nStatus );
	AfxGetApp()->WriteProfileInt( _T("Search"), _T("Library"), m_nLibrary );

	// close dialog
	CDialog::OnOK();
}

void CDlgSearchRequest::OnBnClickedButtonClear()
{
	// clear variables
	m_szFilter.Empty();
	m_szName.Empty();
	m_szEmail.Empty();
	m_szLocation.Empty();
	m_szPhone.Empty();
	m_szFax.Empty();
	m_szInstructions.Empty();
	m_szCitation.Empty();
	m_szNotes.Empty();
	m_szRequestID.Empty();
	m_nStatus = -1;
	m_nLibrary = -1;

	// set selections
	m_ctrlLibrary.SetCurSel( m_nLibrary );
	m_ctrlStatus.SetCurSel( m_nStatus );

	// update data
	UpdateData( FALSE );
}

void CDlgSearchRequest::OnBnClickedButtonMore()
{
	// set more
	m_bMore = !m_bMore;
	// clear
	OnBnClickedButtonClear();
	// size controls
	SizeControls();
}

// CLIttsView functions
BOOL CDlgSearchRequest::SizeControls()
{
	// check if we are created
	if( GetDlgItem( IDOK )->GetSafeHwnd() )
	{
		CRect rectWindow;
		GetWindowRect( rectWindow );
		INT nTx = rectWindow.Width();
		INT nTy = rectWindow.Height();

		// move buttons
		if( m_bMore )
		{			
			INT nWindowY = 530;
			INT nButtonY = nWindowY - 60;

			// size window
			SetWindowPos( NULL, 0, 0, rectWindow.Width(), nWindowY, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE );

			// move buttons
			GetDlgItem( IDC_BUTTON_MORE )->SetWindowPos( NULL, 10, nButtonY, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDC_BUTTON_CLEAR )->SetWindowPos( NULL, 90, nButtonY, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDOK )->SetWindowPos( NULL, 270, nButtonY, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDCANCEL )->SetWindowPos( NULL, 190, nButtonY, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			// move controls
			GetDlgItem( IDC_STATIC_LIBRARY )->SetWindowPos( NULL, 10, nButtonY - (265 + 14), 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDC_COMBO_LIBRARY )->SetWindowPos( NULL, 10, nButtonY - 265, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDC_STATIC_STAT )->SetWindowPos( NULL, 10, nButtonY - (228 + 14), 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDC_COMBO_STATUS )->SetWindowPos( NULL, 10, nButtonY - 228, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			// set text
			GetDlgItem( IDC_BUTTON_MORE )->SetWindowText( _T("Less") );
		}
		else
		{
			// set dialog height
			INT nWindowY = 270;

			// adjust for no LAN ID
			if( !appConfig.m_LDAP.bUseLDAPLogons )
				nWindowY -= 40;

			// set button Y
			INT nButtonY = nWindowY - 60;
			
			// size window
			SetWindowPos( NULL, 0, 0, rectWindow.Width(), nWindowY, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE );

			// move buttons
			GetDlgItem( IDC_BUTTON_MORE )->SetWindowPos( NULL, 10, nButtonY, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDC_BUTTON_CLEAR )->SetWindowPos( NULL, 90, nButtonY, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDOK )->SetWindowPos( NULL, 270, nButtonY, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDCANCEL )->SetWindowPos( NULL, 190, nButtonY, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			// move controls
			GetDlgItem( IDC_STATIC_LIBRARY )->SetWindowPos( NULL, 10, 380, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDC_COMBO_LIBRARY )->SetWindowPos( NULL, 10, 380, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDC_STATIC_STAT )->SetWindowPos( NULL, 10, 380, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			GetDlgItem( IDC_COMBO_STATUS )->SetWindowPos( NULL, 10, 380, 0, 0, SWP_NOZORDER | SWP_NOSIZE );
			// set text
			GetDlgItem( IDC_BUTTON_MORE )->SetWindowText( _T("More") );
		}
	}
	return FALSE;
}

CString CDlgSearchRequest::ES(CString String)
{
	return appConfig.m_SQL.ES( String );
}
